﻿using Coldairarrow.Business.MEGA;
using Coldairarrow.Entity.MEGA;
using Coldairarrow.Entity.TurnPDF;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using Coldairarrow.Util.DbHelpers;
using EFCore.Sharding;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.MEGAAPP
{
    [Route("/MEGAAPP/[controller]/[action]")]
    public class LeafAndMaterialHomeController : BaseApiController
    {
        readonly IMEGA_LeafBladeBusiness _mEGA_LeafBladeBus;
        readonly IOperator _operator;
        readonly IDbAccessor _Db;
        readonly IMEGA_XZQHBusiness _XZQHBusiness;
        readonly IMEGA_LeafBladeBusiness _LeafBladeBusiness;
        #region DI
        public LeafAndMaterialHomeController(IMEGA_LeafBladeBusiness @LeafBladeBusiness,IMEGA_LeafBladeBusiness mEGA_LeafBladeBus, IDbAccessor @Db, IOperator @operator, IMEGA_XZQHBusiness @XZQHBusiness)
        {
            _mEGA_LeafBladeBus = mEGA_LeafBladeBus;
            _Db = Db;
            _operator = @operator;
            _XZQHBusiness = @XZQHBusiness;
            _LeafBladeBusiness = @LeafBladeBusiness;
        }
        #endregion

        #region-----------【首页列表】------------

        /// <summary>
        /// 油漆列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [NoCheckJWT]
        [HttpPost]
        public async Task<object> GetMaterialList(IdInputDTO input)
        {
            return await _mEGA_LeafBladeBus.GetQrCodeByIdDataAsync(input);
        }

        #endregion


        #region------------【叶片油漆首页查询】---------------
        /// <summary>
        /// 叶片首页查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [NoCheckJWT]
        [HttpPost]
        public async Task<ActionResult> GetSearchList(InputSearchDTO input)
        {
            int Flag = input.Flag;
            string KeyWord = input.KeyWord;
            string[] title = { }; //初始化表头
            DataTable dt = new DataTable();
            try
            {
                string strtemp = string.Empty;
                var sql = $"EXEC sp_LeafAndMaterialAndFactoryAndWorkerSearch {Flag};";
                dt = DbHelperSQL.Query(sql).Tables[0];
                dt.Columns.Remove("Start_Time");
                dt.Columns.Remove("End_Time");
                DataTable newdt = dt.Clone();
                switch (Flag)
                {
                    case 1: //油漆查询
                        title = new[] { "油漆批次", "叶片编号", "叶片名称", "加工厂", "时间", "状态" };
                        strtemp = " YQ_Batch like '%" + KeyWord + "%'" + "or YP_Code like '%" + KeyWord + "%'" + "or YP_Name like '%" + KeyWord + "%' or Factory_Name like '%" + KeyWord + "%' ";
                        break;
                    case 2://叶片查询
                        title = new[] { "叶片编号", "叶片名称", "加工厂", "油漆批次", "时间", "状态" };
                        strtemp = " YQ_Batch like '%" + KeyWord + "%'" + "or YP_Code like '%" + KeyWord + "%'" + "or YP_Name like '%" + KeyWord + "%' or Factory_Name like '%" + KeyWord + "%' ";
                        break;
                    case 3: //加工厂查询
                        title = new[] { "加工厂编号", "加工厂", "叶片编号", "叶片名称", "时间", "状态" };
                        strtemp = " Factory_Code like '%" + KeyWord + "%'" + "or YP_Code like '%" + KeyWord + "%'" + "or YP_Name like '%" + KeyWord + "%' or Factory_Name like '%" + KeyWord + "%' ";
                        break;
                    case 4: //加工人查询
                        title = new[] { "姓名", "叶片编号", "叶片名称", "加工厂", "时间", "状态" };
                        strtemp = " Worker_Name like '%" + KeyWord + "%'" + "or YP_Code like '%" + KeyWord + "%'" + "or YP_Name like '%" + KeyWord + "%' or Factory_Name like '%" + KeyWord + "%' ";
                        break;
                }
                DataRow[] dataRow = dt.Select(strtemp);
                foreach (var model in dataRow)
                {
                    newdt.ImportRow(model);
                }
                dt = newdt;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            var res = new
            {
                Title = title,
                Data = dt,
                Success = true
            };
            return JsonContent(res.ToJson());
        }

        #endregion


        #region-----------【首页地图】------------------

        /// <summary>
        /// 地图---获取工厂基础信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetAreaDataList(DepartmentsTreeInputDTO input)
        {
            string xzqhid = _operator.XZQHId;
            //获取该直属名下所有区划信息
            List<string> Ids = GetfarmlandList(xzqhid);
            //获取工厂信息
            var q = _Db.GetIQueryable<MEGA_FactoryManagement>().Where(e => Ids.Contains(e.AreaId)).ToList();
            var res = new
            {
                Data = q,
                Success = true
            };
            return JsonContent(res.ToJson());
        }

        /// <summary>
        /// 地图---获取工厂基础信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<object> GetAreaDataById(IdInputDTO input)
        {
            try
            {
                return await _mEGA_LeafBladeBus.GetQrCodeByIdDataAsync(input);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }


        #endregion

        /// <summary>
        ///APP--扫码叶片、获取叶片二维码信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [NoCheckJWT]
        [HttpPost]
        public async Task<object> GetQrCodeByIdData(IdInputDTO input)
        {
            return await _mEGA_LeafBladeBus.GetQrCodeByIdDataAsync(input);
        }


        /// <summary>
        /// 首页查询模型
        /// </summary>
        public class InputSearchDTO
        {
            /// <summary>
            /// 1 油漆，2 叶片，3 加工厂
            /// </summary>
            public int Flag { get; set; }
 
            /// <summary>
            /// 查询内容
            /// </summary>
            public string KeyWord { get; set; }
        }


        /// <summary>
        /// 获取名下所有子集id
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        private List<string> GetfarmlandList(string parentId)
        {
            var q = _XZQHBusiness.GetChildrenIds(parentId);

            return q;
        }
    }

   
}
